package org.nado.bbs;

import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class BBSServiceImpl implements BBSService {

	private static final Logger logger = Logger.getLogger("BBSServiceImpl");

	@Inject
	BBSDAO dao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public BBSBoardVO registNew(BBSBoardVO vo) throws Exception {
		logger.info("registNew.........." + vo);
		dao.registNew(vo);
		int bbs_no = dao.getBBSNo();

		return dao.get(bbs_no);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public BBSBoardVO registUp(BBSBoardVO vo) throws Exception {
		logger.info("registUp.........." + vo);
		dao.registGordUpdate(vo);
		dao.registOld(vo);
		int bbs_no = dao.getBBSNo();
		return dao.get(bbs_no);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public BBSBoardVO get(Integer bbs_no) throws Exception {
		logger.info("get............." + bbs_no);
		dao.readCntUpdate(bbs_no);
		return dao.get(bbs_no);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void modify(BBSBoardVO vo) throws Exception {
		logger.info("modify............." + vo);
		dao.modify(vo);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<BBSBoardVO> list(Integer page_no) throws Exception {
		logger.info("list............." + page_no);
		return dao.list(page_no);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void replyResist(ReplyVO vo) throws Exception {
		logger.info("replyResist............." + vo);
		dao.replyRegist(vo);
		dao.replyUpdateRecnt(vo);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void replyDelete(ReplyVO vo) throws Exception {
		logger.info("replyResist............." + vo);
		dao.replyDelete(vo);
		dao.replyDisconutRecnt(vo);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<BBSBoardVO> search(String type, String keyword, Integer page_no)
			throws Exception {
		logger.info("search............." + type + keyword + page_no);
		HashMap<String, Object> valueMap = new HashMap<String, Object>();

		valueMap.put("type", type);
		valueMap.put("keyword", "%" + keyword + "%");
		valueMap.put("page_no", page_no);

		return dao.search(valueMap);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<BBSBoardVO> dynamicSearch(SearchVO vo) throws Exception {
		logger.info("search............." + vo.toString());
		SearchVO dynamicVo = new SearchVO();
		if (vo.getBbs_content() != null) {
			dynamicVo.setBbs_content("%" + vo.getBbs_content() + "%");
		}
		if (vo.getBbs_title() != null) {
			dynamicVo.setBbs_title("%" + vo.getBbs_title() + "%");
		}
		if (vo.getBbs_writer() != null) {
			dynamicVo.setBbs_writer("%" + vo.getBbs_writer() + "%");
		}
		dynamicVo.setPage_no(vo.getPage_no());

		return dao.dynamicList(dynamicVo);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void remove(Integer bbs_no, Integer bbs_pno) throws Exception {
		logger.info("remove............" + bbs_no);
		List<String> cnt = null;
		if (bbs_pno != 0) {
			cnt = dao.getContentCnt(bbs_no);
			logger.info("-------------------------------------cnt: " + cnt);
		}
		if (cnt == null || cnt.isEmpty()) {
			dao.removeAllReply(bbs_no);
			dao.removeRe(bbs_no);
			logger.info("..................cnt == 0");
		} else {
			dao.removeAllReply(bbs_no);
			dao.removeOri(bbs_no);
			logger.info("..................cnt > 0");
		}

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<ReplyVO> replyList(Integer bbs_rep_no) throws Exception {
		return dao.getReplyList(bbs_rep_no);
	}

}
